import {shopping, shopPost} from '@/api/api.js'

export function objectDate (url, date) {
	const params = {
		s: url,
		wxapp_id: '10001',
		token: uni.getStorageSync('Authori-shop'),
	}
	for (let i in date) {
		params[i] = date[i]
	}
	return params
}

function objFormData (date) {
	const params = {
		wxapp_id: '10001',
		token: uni.getStorageSync('Authori-shop'),
	}
	for (let i in date) {
		params[i] = date[i]
	}
	return params
}

// 产品分类
export function tabs (date) {
	const param = objectDate('/api/category/index', date)
	return shopping('', param, 'GET')
}

// 商品分类
export function shopList (date) {
	const param = objectDate('/api/goods/lists', date)
	return shopping('', param, 'GET')
}

// 商品详情
export function details (date) {
	const param = objectDate('/api/goods/detail', date)
	return shopping('', param, 'GET')
}

// 加入购物车
export function cardAdd (date) {
	const formDate = objFormData(date)
	return shopPost('/api/cart/add', formDate, 'POST')
}

// 购物车减数量
export function cardsub (date) {
	const formDate = objFormData(date)
	return shopPost('/api/cart/sub', formDate, 'POST')
}

// 立即购买
export function buyNow (date) {
	const param = objectDate('/api/order/buyNow', date)
	return shopping('', param, 'GET')
}

// 购物车列表
export function cartList (date) {
	const param = objectDate('/api/cart/lists', date)
	return shopping('', param, 'GET')
}

// 获取商家ID
export function shopPayDetail (date) {
	const param = objectDate('/api/shop/shopPayDetail', date)
	return shopping('', param, 'GET')
}

// 获取商家详情
export function shopDetail (date) {
	const param = objectDate('/api/shop/detail', date)
	return shopping('', param, 'GET')
}

// 收货地址列表
export function addressList (date) {
	const formDate = objFormData(date)
	return shopPost('/api/address/lists', formDate, 'POST')
}

// 新增地址
export function addAddress (date) {
	const formDate = objFormData(date)
	return shopPost('/api/address/add', formDate, 'POST')
}

// 设置默认地址
export function setDefault (date) {
	const formDate = objFormData(date)
	return shopPost('/api/address/setDefault', formDate, 'POST')
}

// 删除地址
export function removeAddress (date) {
	const formDate = objFormData(date)
	return shopPost('/api/address/delete', formDate, 'POST')
}

// 获取地址详情
export function addressDetails (date) {
	const param = objectDate('/api/address/detail', date)
	return shopping('', param, 'GET')
}

// 编辑地址详情
export function addressEdit (date) {
	const formDate = objFormData(date)
	return shopPost('/api/address/edit', formDate, 'POST')
}

// 提交订单
export function submitBuy (date) {
	const formDate = objFormData(date)
	return shopPost('/api/order/buyNow', formDate, 'POST')
}

// 购物车确认订单
export function cartConfirm (date) {
	const param = objectDate('/api/order/cart', date)
	return shopping('', param, 'GET')
}

// 购物车提交订单
export function cartSubmit (date) {
	const formDate = objFormData(date)
	return shopPost('/api/order/cart', formDate, 'POST')
}

// 收支明细
export function payList (date) {
	const param = objectDate('/api/balance.log/lists', date)
	return shopping('', param, 'GET')
}

// 个人中心
export function myCenter (date) {
	const param = objectDate('/api/user.index/detail', date)
	return shopping('', param, 'GET')
}

// 提现明细
export function withdrawList (date) {
	const param = objectDate('/api/user.balance.withdraw/lists', date)
	return shopping('', param, 'GET')
}

// 关于公司
export function aboutCompany (date) {
	const param = objectDate('/api/Article/lists', date)
	return shopping('', param, 'GET')
}

// 订单列表
export function orderList (date) {
	const param = objectDate('/api/user.order/lists', date)
	return shopping('', param, 'GET')
}

// 取消订单
export function cancelOrder (date) {
	const param = objectDate('/api/user.order/cancel', date)
	return shopping('', param, 'GET')
}

// 收款码
export function shop_qrcode (date) {
	const param = objectDate('/api/shop/shop_qrcode', date)
	return shopping('', param, 'GET')
}

// 编辑用户信息
export function editUser (date) {
	const param = objectDate('/api/user.index/editUserDetail', date)
	return shopping('', param, 'GET')
}

// 钱包
export function wallet (date) {
	const param = objectDate('/api/user.wallet/index', date)
	return shopping('', param, 'GET')
}

// 商家列表
export function shopLists (date) {
	const param = objectDate('/api/shop/lists', date)
	return shopping('', param, 'GET')
}

// 确定收货
export function receipt (date) {
	const formDate = objFormData(date)
	return shopPost('/api/user.order/receipt', formDate, 'POST')
}

// 订单列表支付
export function onePay (date) {
	const param = objectDate('/api/order/onePay', date)
	return shopping('', param, 'GET')
}

// 登录
export function login (date) {
	const uid = uni.getStorageSync('userInfo').uid;
	const avatarUrl = uni.getStorageSync('userInfo').avatar;
	const nickName = uni.getStorageSync('userInfo').nickname;
	return shopPost('/api/user/login', {
		wxapp_id: 10001,
		code: uid,
		avatarUrl: avatarUrl,
		nickName: nickName
	}, 'POST')
}


// 判断是否登录
export function isLogin () {
	// 判断是否授权
	if (uni.getStorageSync('userInfo') === '') {
		uni.navigateTo({
			url: '/pages/login/login'
		})
	} else {
		let token = ''
		if (uni.getStorageSync('Authori-shop') === '') {
			login('').then(res => {
				uni.setStorageSync('Authori-shop', res.data.token)
				token = res.data.token
			})
		}
	}
}